import logging
import getpass
import sys

class MyLog(object):
    def __init__(self):
        print('初始化开始')
        user = getpass.getuser();
        self.logger = logging.getLogger(user);
        self.logger.setLevel(logging.DEBUG);
        logFile = sys.argv[0][0:-3] + '.log'
        formatter = logging.Formatter()
        
        print(logFile)
        print(formatter)

        logHand = logging.FileHandler(logFile);
        logHand.setFormatter(formatter);
        logHand.setLevel(logging.DEBUG);

        logHandSt = logging.StreamHandler();
        logHandSt.setFormatter(formatter);

        self.logger.addHandler(logHand);
        self.logger.addHandler(logHandSt)
        print('初始化成功')

    def debug(self,msg):
        self.logger.debug(msg);

    def info(self,msg):
        self.logger.info(msg);

    def warn(self,msg):
        self.logger.warn(msg)

    def error(self,msg):
        self.logger.error(msg)

    def critical(self,msg):
        self.logger.critical(msg)

if __name__ == '__main__':
    user = getpass.getuser();
    print(user)
    print(logging.getLogger(user))

    mylog = MyLog();
    mylog.debug('debug');
    mylog.info('info')
    mylog.warn('warn')
    mylog.error('error')
    mylog.critical('critical')

